import domain.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.jupiter.api.Test;
import urlBase.URLBase;
import urlBase.impl.RedisBase;
import utils.DBCPUtils;

import java.io.IOException;
import java.sql.SQLException;

public class test {

    public void testDBCPUtil() throws SQLException {
        System.out.println(DBCPUtils.getConnection());
        //_____________________________________________
        //DBUtils框架，对JDBC轻量级的封装
        //需求：要获得demo数据库中的表user_中所有记录的条数
        QueryRunner qr = new QueryRunner(DBCPUtils.getDataSource());
        //BeanListHandler ~> 将表中每一条记录都封装到实例中，且将实例置于List容器中
        //BeanHandler ~> 将表中指定的唯一一条记录封装成实例
        //ScalarHandler ~>求表记录的条数
        String host = qr.query("select *  from mysql.user", new ScalarHandler<>("host"));
        System.out.println(host);
    }

    public void getItem(String url) throws IOException {
        if (url.matches("https://item.jd.com/.*")) {
            Crawler c = new Crawler();
            Page page = c.get(url);
            c.parse(page);
            System.out.println(page.getTitle());
        }
    }

    public void start() throws IOException, InterruptedException {
        URLBase urlbase = RedisBase.getURLBase();
        urlbase.addIndex("https://www.jd.com/allSort.aspx");
        while (true) {
            String index = urlbase.getIndex();
            Document doc = Jsoup.connect(index).get();
            urlbase.discard(index);
            doc.select("a[href]")
                    .forEach(element ->
                    {
                        String href = element.attr("href");
                        if (!href.startsWith("https:")) {
                            href = "https:" + href;
                        }
                        if (href.matches("https://list.jd.com/.*")) {
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            if (!urlbase.exist(href)) {
                                urlbase.addIndex(href);
                            }
                        } else if (href.matches("https://item.jd.com/.*.html")) {
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            if (!urlbase.exist(href)) {
                                urlbase.addItem(href);
                            }
                        }
                    });
//            HashSet<String> items = new HashSet<>();
//            LocalBase.item.drainTo(items);
//            items.forEach(itemURL -> {
//                if (!urlbase.exist(itemURL)) {
//                    try {
//                        getItem(itemURL);
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                    urlbase.discard(itemURL);
//                }
//            });

            for (int i = 0; i < urlbase.size(); i++) {
                String itemURL = urlbase.getItem();
                Thread.sleep(100);
                if (!urlbase.exist(itemURL)) {
                    getItem(itemURL);
                    urlbase.discard(itemURL);
                }
                urlbase.discard(itemURL);
            }
        }
    }
}

